#include "mainwidget.h"
#include <QMessageBox>
#include <QThread>
#include <QDebug>

mainWidget::mainWidget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("UDP Client"));

    //左侧布局
    ReceiveTextEdit = new QTextEdit(this);
    ChatBtn = new QPushButton(tr("Chat"), this);
    SendLineEdit = new QLineEdit();
    QGridLayout *left = new QGridLayout();
    left->addWidget(ReceiveTextEdit, 0, 0, 4, 4);
    left->addWidget(SendLineEdit, 4, 0, 1, 3);
    left->addWidget(ChatBtn, 4, 3, 1, 1);


    //右侧布局
    nameLabel = new QLabel(tr("Name:"));
    nameLineEdit = new QLineEdit();
    portLabel = new QLabel(tr("MyPort:"));
    portLineEdit = new QLineEdit();
    portLineEdit->setInputMask("9999");
    ServerAddressLabel = new QLabel(tr("Server Address:"));
    ServerAddressLineEdit = new QLineEdit();
    ServerAddressLineEdit->setInputMask("000.000.000.000;");
    ServerPortLabel = new QLabel(tr("Server Port:"));
    ServerPortLineEdit = new QLineEdit();
    ServerPortLineEdit->setInputMask("9999");
    LoginBtn = new QPushButton(tr("Login"));
    QuitBtn = new QPushButton(tr("Quit"));
    CloseBtn = new QPushButton(tr("Close"));
    QGridLayout *right = new QGridLayout();
    right->addWidget(nameLabel, 0, 0);
    right->addWidget(nameLineEdit, 0, 1);
    right->addWidget(portLabel, 1, 0);
    right->addWidget(portLineEdit, 1, 1);
    right->addWidget(ServerAddressLabel, 2, 0);
    right->addWidget(ServerAddressLineEdit, 2, 1);
    right->addWidget(ServerPortLabel, 3, 0);
    right->addWidget(ServerPortLineEdit, 3, 1);
    right->addWidget(LoginBtn, 4, 0);
    right->addWidget(QuitBtn, 4, 1);
    right->addWidget(CloseBtn, 5, 0, 1, 2);

    //总体布局
    mainLayout = new QHBoxLayout(this);
    mainLayout->addLayout(left);
    mainLayout->addLayout(right);

    connect(CloseBtn, SIGNAL(clicked()), this, SLOT(CloseBtnClicked()));
    connect(LoginBtn, SIGNAL(clicked()), this, SLOT(doLogin()));
    connect(QuitBtn, SIGNAL(clicked()), this, SLOT(doQuit()));
    connect(ChatBtn, SIGNAL(clicked()), this, SLOT(doChat()));

    //初始化各个按键
    LoginBtn->setEnabled(true);
    CloseBtn->setEnabled(true);
    ChatBtn->setEnabled(false);
    QuitBtn->setEnabled(false);
    portLineEdit->setText(tr("5555"));
    ServerAddressLineEdit->setText(tr("127.0.0.1"));
    ServerPortLineEdit->setText(tr("8888"));


}

mainWidget::~mainWidget()
{
}

void mainWidget::CloseBtnClicked()
{
    close();
}

void mainWidget::dataRecv()
{
    while(udpSocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(), datagram.size());

        QString msg = datagram.data();
//        ReceiveTextEdit->insertPlainText(msg);
        ReceiveTextEdit->append(msg);
    }
}

void mainWidget::doLogin()
{
    if(ServerPortLineEdit->text().isEmpty() || ServerAddressLineEdit->text().isEmpty())
    {
        QMessageBox::information(this, tr("error"), tr("Server infomation is NULL!"));
        return ;
    }
    ServerAddress = QHostAddress(ServerAddressLineEdit->text());
    Serverport = ServerPortLineEdit->text().toInt();
    if(portLineEdit->text().isEmpty())
    {
        QMessageBox::information(this, tr("error"), tr("My Port infomation is NULL!"));
        return ;
    }
    Clientport = portLineEdit->text().toInt();

    if(nameLineEdit->text().isEmpty())
    {
        QMessageBox::information(this, tr("error"), tr("My Name infomation is NULL!"));
        return ;
    }

    //发送注册登录信息
    udpSocket = new QUdpSocket(this);
    connect(udpSocket, SIGNAL(readyRead()), this, SLOT(dataRecv()));
    bool result = udpSocket->bind(Clientport);
    if(!result)
    {
        QMessageBox::information(this, tr("error"), tr("udp socket create error!"));
        return ;
    }
    LoginBtn->setEnabled(false);
    QuitBtn->setEnabled(true);
    ChatBtn->setEnabled(true);
    memcpy(sendmsg.name, nameLineEdit->text().toLatin1(), nameLineEdit->text().length());
    sendmsg.key = LOGIN;

    //发送给服务器
    qDebug() << "send Login data!";
    udpSocket->writeDatagram((char *)&sendmsg, sizeof(sendmsg), ServerAddress, Serverport);
}

void mainWidget::doChat()
{
    if(SendLineEdit->text().isEmpty())
    {
        QMessageBox::information(this, tr("error"), tr("Send Msg is NULL!"));
        return ;
    }
    sendmsg.key = CHAT;
    memset(sendmsg.data, 0, sizeof(sendmsg.data));
    memcpy(sendmsg.data, SendLineEdit->text().toLatin1(), SendLineEdit->text().length());

    //发送给服务器
    qDebug() << "send Login data!";
    udpSocket->writeDatagram((char *)&sendmsg, sizeof(sendmsg), ServerAddress, Serverport);
}

void mainWidget::doQuit()
{
    sendmsg.key = QUIT;
    memset(sendmsg.data, 0, sizeof(sendmsg.data));

    //发送给服务器
    qDebug() << "send Login data!";
    udpSocket->writeDatagram((char *)&sendmsg, sizeof(sendmsg), ServerAddress, Serverport);

    //等待服务端做出反馈后再显示到画面上
    QThread::msleep(500);
    QuitBtn->setEnabled(false);
    ChatBtn->setEnabled(false);
    LoginBtn->setEnabled(true);
    udpSocket->close();
}
